import common.commonFunction;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.LinkButton;
import mx.controls.NumericStepper;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.validators.Validator;

[Embed(source="images/error.png")]private var errorIcon:Class;
[Embed(source="images/success.png")]private var successIcon:Class;
[Embed(source="images/reset.png")]private var resetIcon:Class;
			[Bindable]public var t:LinkButton=new LinkButton();
			
			
			[Bindable]public var registrationText:NumericStepper=new NumericStepper();
			[Bindable]public var addDropText:NumericStepper=new NumericStepper();
			[Bindable] private var programDataList:ArrayCollection = new ArrayCollection();
			[Bindable]protected var sessionDate:String="";
			 public var url_DNS:String ="";
			[Bindable] public var urlLoadSession:String = "";
			[Bindable] public var xmlDataLoadSession:XML;
			[Bindable] public var urlLoadProgramDetails:String ="";
			[Bindable] public var xmlDataLoadProgramDetails:XML;
			[Bindable] public var urlInsertProgramDetails:String = "";
			[Bindable] public var xmlInsertionMessage:XML;

			
			public function init():void{
//				sessionDate = "anil";
				url_DNS = commonFunction.getConstants('url');				
    		urlLoadSession = url_DNS+"/programregistration/loadSession.htm";
    		urlLoadProgramDetails = url_DNS+"/programregistration/programToRegister.htm";
    		urlInsertProgramDetails = url_DNS+"/programregistration/insertProgram.htm";
    		getCurrentSession();
    		t.enabled=false;
			}
			
			
		
		
		public function getCurrentSession():void {
       	var params:Object = {};
     		 params["abc"] =new Date().getTime();//does not use of params with this send(), it is only used for affect the page contents according dbase
    	    httpLoadSession.send(params);
        }
        
       public function getProgramDetails():void {
       	var params:Object = {};
     		 params["abc"] ="anil";//does not use of params with this send(), it is only used for affect the page contents according dbase
    	     httpLoadProgramDetails.send(params);
        }
        
        private function resultLoadSessionHandler(event:ResultEvent):void{
        xmlDataLoadSession = event.result as XML;
        if(xmlDataLoadSession.sessionConfirm == true)
    		    {
        		Alert.show(resourceManager.getString('Messages','sessionInactive'));
        		this.parentDocument.vStack.selectedIndex=0;
				this.parentDocument.loaderCanvas.removeAllChildren();
        		}
        sessionDate = xmlDataLoadSession.sessionDate;
        getProgramDetails();
       }
       
       
        private function resultLoadProgramDetailsHandler(event:ResultEvent):void{
        xmlDataLoadProgramDetails = event.result as XML;
         if(xmlDataLoadProgramDetails.sessionConfirm == true)
    		    {
        		Alert.show(resourceManager.getString('Messages','sessionInactive'));
        		this.parentDocument.vStack.selectedIndex=0;
				this.parentDocument.loaderCanvas.removeAllChildren();
        		}
      //Alert.show(xmlDataLoadProgramDetails);
       // sessionDate = xmlDataLoadSession.sessionDate;
       loadProgramRegistrationList();
       }
       
       private function resultInsertProgramHandler(event:ResultEvent):void{
        xmlInsertionMessage = event.result as XML;
        if(xmlInsertionMessage.sessionConfirm == true)
    		    {
        		Alert.show(resourceManager.getString('Messages','sessionInactive'));
        		this.parentDocument.vStack.selectedIndex=0;
				this.parentDocument.loaderCanvas.removeAllChildren();
        		}
        if(xmlInsertionMessage.message=='success'){
       	Alert.show(commonFunction.getConstants('insertSuccessful'),
       	commonFunction.getConstants('success'),0,null,null,successIcon);
       	getProgramDetails();
//       	getExistingEntityDetails();
//       	getProgramBranchSpeclList();
        }
        else if(xmlInsertionMessage.message=='fail'){
       	Alert.show(commonFunction.getConstants('insertFailed'),
       	commonFunction.getConstants('fail'),0,null,null,errorIcon);
       }
     }
             
        private function faultLoadSessionHandler(event:FaultEvent):void{
         mx.controls.Alert.show(event.fault.message,commonFunction.getConstants('errorInResult'));
    }
		
		private function loadProgramRegistrationList():void{
                 	programDataList.removeAll();
                  	 for each(var s:Object in xmlDataLoadProgramDetails.programCourseKey){
                  	 	//Alert.show(s.program.@name);
                  programDataList.addItem({select:false,programName:s.program.@name,programId:s.program.programId,
                  branchName:s.branch.@name,branchId:s.branch.branchId,
                  specializationName:s.specialization.@name,specializationId:s.specialization.specializationId,
                  semesterCode:s.semesterCode,semesterStartDate:s.semesterStartDate,
                  semesterEndDate:s.semesterEndDate,programCourseKey:s.@name});
                  }
                }	
	
	//on Click of Submit Button
	public function submitForm():void
	{
		if(validateScreen())
		{

			for each(var obj:Object in programRegistrationDetailsGrid.dataProvider)
	        {
	        	
		        if(obj.select==true)
		        { 
		        	var params:Object = {};
     		 		params["programCourseKey"] = obj.programCourseKey;
     		 		params["semesterStartDate"] = obj.semesterStartDate;
     		 		params["semesterEndDate"] = obj.semesterEndDate;
     		 		params["registrationPeriod"] = registrationText.value;
     		 		params["addDropPeriod"] = addDropText.value;
     		 		
     		 		httpInsertProgramDetails.send(params);

			    }
			    
			 }
		}
	}	
	//validating user form
public function validateScreen():Boolean
{
	var b:Boolean=true;
	var selectedCount:int=0;
	var mentorErrorCount:int=0;
	var seatsErrorCount:int=0;
	var errorString:String="";
	for each(var obj:Object in programRegistrationDetailsGrid.dataProvider)
	{
		if(obj.select==true)
		{
			selectedCount++;
			registrationText.value = obj.registrationPeriod;
			addDropText.value = obj.addDropPeriod;
			registrationValidator.source = registrationText;
			addDropValidator.source = addDropText;
			if(Validator.validateAll([registrationValidator]).length!=0)
			{
				b=false;
				Alert.show(commonFunction.getMessages('pleaseFillRegistrationPeriod'),commonFunction.getMessages('error'),0,null,null,errorIcon);
			}
			else if(Validator.validateAll([addDropValidator]).length!=0)
			{
				b=false;
				Alert.show(commonFunction.getMessages('pleaseFillAddDropPeriod'),commonFunction.getMessages('error'),0,null,null,errorIcon);
			}

		}
	}
//	if(seatsErrorCount>0)
//	{
//		b=false;
//		//errorString=commonFunction.getMessages('pleasecheckEntriesinRed');
//		Alert.show(commonFunction.getMessages('pleasecheckEntriesinRed'),commonFunction.getMessages('error'),0,null,null,errorIcon);
//	}
	
	
//	if(errorString!="")
//	{
//		Alert.show(errorString,commonFunction.getMessages('error'),0,null,null,errorIcon);
//	}
	return b;
}		
			

			